export type ClassValue = string | number | boolean | undefined | null | { [className: string]: any } | ClassArray;
export type ClassArray = Array<ClassValue>;

/**
 * 根据条件生成 className 字符串
 * @param args - 类名参数
 * @returns 合并后的类名字符串
 */
export function classNames(...args: ClassValue[]): string {
  const classes: string[] = [];

  for (const arg of args) {
    if (!arg) continue;

    const argType = typeof arg;

    if (argType === 'string' || argType === 'number') {
      classes.push(arg.toString());
    } else if (Array.isArray(arg)) {
      if (arg.length) {
        const inner = classNames.apply(null, arg);
        if (inner) {
          classes.push(inner);
        }
      }
    } else if (argType === 'object') {
      if (arg.toString === Object.prototype.toString) {
        for (const key in arg as { [key: string]: any }) {
          if (arg.hasOwnProperty(key) && (arg as { [key: string]: any })[key]) {
            classes.push(key);
          }
        }
      } else {
        classes.push(arg.toString());
      }
    }
  }

  return classes.join(' ');
}

export default classNames;